import math
from libavg import avg
from trash import Trash
from config import Config

# this trash object is destroyable by dragging it out of the board
class DragableTrash(Trash):

    def __init__(self, x, par, weight, img):
        Trash.__init__(self, x, par, weight, img);  # KONSTRUKTOR DER OBERKLASSE TRASH 
        self.__captureHolder = None
        self.__dragOffsetX = 0
        self.__dragOffsetY = 0
        self.setEventHandler(avg.CURSORDOWN, avg.TOUCH, self.__enter)
        self.setEventHandler(avg.CURSORMOTION, avg.TOUCH, self.__capture)
        self.setEventHandler(avg.CURSORUP, avg.TOUCH, self.__leave) 

    def __enter(self, event):
        self.__updateable=False
        if self.anim is not None:
            self.anim.abort()
        if self.__captureHolder is None:
            self.__captureHolder = event.cursorid
            self.__dragOffsetX = event.node.pos.x - event.pos.x
            self.__dragOffsetY = event.node.pos.y - event.pos.y
            event.node.setEventCapture(event.cursorid)
            
    def __capture(self, event):
        if event.cursorid == self.__captureHolder:
            self.pos = event.pos + avg.Point2D(self.__dragOffsetX, self.__dragOffsetY)
            if self.x <= 0 or self.x+self.width >= self.getParent().width:
                self.releaseEventCapture(event.cursorid)
                self.__captureHolder = None
                self.fadeOut()
        
    def __leave(self, event):
        self.__updateable=True
        if event.cursorid == self.__captureHolder:
            event.node.releaseEventCapture(event.cursorid)
            self.__captureHolder = None
            self.fallDown(self.wind) # NACH LOSLASSEN MIT ZULETZT BEKANNTER WINDRICHTUNG FALLEN